package com.gitee.blogcase.gracepaycase.template;

import com.gitee.blogcase.gracepaycase.dto.PayDTO;
import com.gitee.blogcase.gracepaycase.container.PayBeanContainer;
import com.gitee.blogcase.gracepaycase.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付模板
 *
 * @author wentao.wu
 * @date 2022/01/04
 */
@Component
public class PayTemplate {

    @Autowired
    private PayBeanContainer payBeanContainer;

    /**
     * 支付
     *
     * @param payDTO 支付dto
     * @return boolean
     */
    public <T> boolean pay(PayDTO<T> payDTO) {
        if (!payBeanContainer.checkBean(payDTO.getMode())) {
            throw new RuntimeException("平台不支持该支付方式，非法调用!");
        }
        PayService server = payBeanContainer.getBean(payDTO.getMode());
        server.befor(payDTO);
        boolean result = server.invoke(payDTO);
        if (result) {
            server.okAfter(payDTO);
        } else {
            server.errorAfter(payDTO);
        }
        return result;
    }

}
